package com.lishe.lishe.lishe.module.auth.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.auth.view.SendCodeTextView;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/5.
 */

public class ActiveFragment extends AuthBaseFragment implements View.OnClickListener {

    public static final String TAG = ActiveFragment.class.getSimpleName();

    ViewPager viewPager = null;

    private EditText inputText1, inputText2;

    private SendCodeTextView mInputText_sendCode;


    public ActiveFragment() {
    }

    public static ActiveFragment newInstance(){
        ActiveFragment instance = new ActiveFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View containerFragment = inflater.inflate(R.layout.activity_auth_active, container, false);

        ImageButton back = (ImageButton) containerFragment.findViewById(R.id.back_title);
        back.setOnClickListener(this);

        inputText1 = (EditText) containerFragment.findViewById(R.id.inputText1);
        inputText1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        inputText1.setText("");
        
        inputText2 = (EditText) containerFragment.findViewById(R.id.inputText2);
        inputText2.setText("");

        Button submitBtn = (Button) containerFragment.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);

        TextView redirectBtn1 = (TextView) containerFragment.findViewById(R.id.redirectBtn1);
        redirectBtn1.setClickable(true);
        redirectBtn1.setOnClickListener(this);

        mInputText_sendCode = (SendCodeTextView) containerFragment.findViewById(R.id.inputText_sendCode);
        mInputText_sendCode.setClickable(true);
        mInputText_sendCode.setOnClickListener(this);

        //AuthHttpHelper.phoneOfActive = null;




        return containerFragment;
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.back_title) {
//            if (viewPager != null) {
//                viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Login, true);
//            }
            if(mJumper!=null)
                mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Login);

        } else if (view.getId() == R.id.redirectBtn1) {

            //用户协议
            if(mJumper!=null)
                mJumper.jumpToFragmentForward(FragmentAuthContants.TAB_INDEX_UserProtocol);


        }else if (view.getId() == R.id.inputText_sendCode) {

            //获取验证码
            AuthHttpHelper.phoneOfActive = null;

            inputText2.setText("");

            final String phone = inputText1.getText().toString().trim();

            if (phone == null || phone.equals("")) {
                Toast.makeText(getActivity(), "请输入手机号码", Toast.LENGTH_SHORT).show();
                return;
            }

            //验证手机号码
            if (!ValidationUtils.isMobileNO(phone)) {
                Toast.makeText(getActivity(), "手机号码格式有误！", Toast.LENGTH_SHORT).show();
                return;
            }

            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("mobile", phone);
            AuthHttpHelper.doCheckActivate(getActivity(), params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {

                    AuthHttpHelper.phoneOfActive = phone;
                    mInputText_sendCode.startCountDown();
                }

                @Override
                public void onFailure(String message) {
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                    mInputText_sendCode.prepareReSendCode();
                }
            });


        } else if (view.getId() == R.id.submitBtn) {

            final String phone = AuthHttpHelper.phoneOfActive;
            final String checkCode = inputText2.getText().toString().trim();
            String phone_input = inputText1.getText().toString().trim();//手机号码输入框


            if(phone_input == null || phone_input.equals("")||!ValidationUtils.isMobileNO(phone_input))
            {
                Toast.makeText(getActivity(), "手机号码格式有误!", Toast.LENGTH_SHORT).show();
                return;
            }

            if(checkCode == null || checkCode.equals(""))
            {
                Toast.makeText(getActivity(), "验证码不能为空！", Toast.LENGTH_SHORT).show();

                return;
            }

            if(phone_input == null ||!phone_input.equals(phone))
            {

                Toast.makeText(getActivity(), "发送验证码的手机号码与当前号码不符！", Toast.LENGTH_SHORT).show();

                return;
            }

            AuthHttpHelper.checkCodeOfActive = checkCode;
            AuthHttpHelper.phoneOfActive = phone;
            inputText1.setText("");
            inputText2.setText("");

            //viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_ActiveEnd,true);
            if(mJumper!=null)
                mJumper.jumpToFragmentForward(FragmentAuthContants.TAB_INDEX_ActiveEnd);

        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


}
